﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Timeline;
using Timeline.Models;


namespace Timeline.Controllers
{
    public class TimelineController : Controller
    {
        /* put's the standardlength of the timeline to 42 days */
        int StandardDuration = 42;
        /* put's the standard start date to today */
        DateTime StandardStartDate = DateTime.Today;
        //
        // GET: /Timeline/

        public ActionResult Index()
        {
            /* get's the variables in the http get request value */
            String StringDocent = Request.QueryString["docent"];
            if (String.IsNullOrEmpty(StringDocent))
            {
                StringDocent = "12345678";
            } 
            String StringDate = Request.QueryString["datum"];
            String StringStudent = Request.QueryString["student"];
            String StringDuration = Request.QueryString["timeframe"];
            /* convert the string with the teacher information to an integer */
            int Docent = Convert.ToInt32(StringDocent);
            /* Check whether the start and duration are explicitly specified in the http get request */
            if (!String.IsNullOrEmpty(StringDate))
            {
                /* convert the startdate from the hhtp get request to date time */
                StandardStartDate = Convert.ToDateTime(StringDate);
            }
            if( !String.IsNullOrEmpty(StringDuration))
            {
                /* convert the duration form the http get request to int */
                StandardDuration = Convert.ToInt32(StringDuration);
                
            }
            /* put all the events for duration to the viewbag */
            EventsModel evenementenModel = GetEventsForMonthAfterDate(Docent, StandardStartDate, StandardDuration);
            /* get all the students for active teacher */
            IEnumerable<String> Students = GetStudentenForSupervisor(Docent);
            /* viewbag the students for the teacher */
            ViewBag.Studenten = Students;
            /* get the events for each student */
            foreach (String ThisStudent in Students)
            {
                /* post the events for every student to the viewbag */
                ViewBag.ThisStudent = GetEventsForStudent(Docent, "09066713");
                
            }
            /* return the teacher*/
            ViewBag.Docent = Docent;

           

            return View(evenementenModel);
        }

        /// <summary>
        /// Get the students for a supervisor where a student is assigned to a supervisor.
        /// </summary>
        /// <param name="SupervisorID">the id of the supervisor you want to know the assigned students for</param>
        /// <returns>A <IEnumerable>String</IEnumerable> with student id's </returns>
        public IEnumerable<string> GetStudentenForSupervisor(int SupervisorID)
        {
            TimelineClassesDataContext database = new TimelineClassesDataContext();
            IEnumerable<String> Students = from a in database.Assignments
                                            where a.FirstSupervisor == SupervisorID|| a.SecondSupervisor == SupervisorID
                                            select a.Student;
            return Students;
        }

        public IEnumerable<Event> GetEventsForSupervisor(int SupervisorID)
        {
            TimelineClassesDataContext database = new TimelineClassesDataContext();
            IEnumerable<Event> events = from a in database.Assignments
                                        join s in database.Students on a.Student equals s.Number
                                        join e in database.Events on a.Number equals e.Assignment
                                        where a.FirstSupervisor == SupervisorID || a.SecondSupervisor == SupervisorID
                                        select e;
            database.Connection.Close();
            return events;
            
        }

        /// <summary>
        /// Get the events for a supervisor for a specific day
        /// </summary>
        /// <param name="SupervisorID">the number of the supervisor</param>
        /// <param name="Date">De Datum waarvoor de events opgehaald dienen te worden.</param>
        /// <returns><IEnumerable>Event</IEnumerable> for a specific date </returns>
        public IEnumerable<Event> GetEventsForDate(int SupervisorID, DateTime Date)
        {
            IEnumerable<Event> eventsForSupervisor = GetEventsForSupervisor(SupervisorID);
            IEnumerable<Event> eventsForDate = from e in eventsForSupervisor
                                               where e.Date == Date
                                               select e;
            return eventsForDate;
        }

        /// <summary>
        /// Get all the events for a specific Student 
        /// </summary>
        /// <param name="SuperVisorID">The nubmer of the supervisor</param>
        /// <param name="StudentID">The value of the studentnumber(must be a string cause it can start with a 0 value on the first character)</param>
        /// <returns>The events for the student</returns>
        public IEnumerable<Event> GetEventsForStudent(int SuperVisorID, String StudentID)
        {
            TimelineClassesDataContext database = new TimelineClassesDataContext();
            IEnumerable<Event> eventsForSupervisor = GetEventsForSupervisor(SuperVisorID);
            IEnumerable<Event> eventsForStudent =   from e in eventsForSupervisor
                                                    join a in database.Assignments on e.Assignment equals a.Number
                                                    where a.Student == StudentID
                                                    select e;
            database.Connection.Close();
            return eventsForStudent;

        }
        /// <summary>
        /// Get the Events for a certain timeframe for a specific teacher
        /// </summary>
        /// <param name="SupervisorID">THe number of the supervisor you need the events off</param>
        /// <param name="date">Starting date of after which to get all the events</param>
        /// <param name="dagen">Duration of the timeframe</param>
        /// <returns>Filled EventsModel with list of eventdates</returns>
        public EventsModel GetEventsForMonthAfterDate(int SupervisorID, DateTime date, int dagen)
        {
            /* Initiliasise the EventsModel*/
            EventsModel eventsforPeriod = new EventsModel();
            /* Put the start date for the EventsModel */
            eventsforPeriod.startDate = date;
            /* put the Duration for the EventsModel */
            eventsforPeriod.Duration = dagen;
            /* initialise the EventsDateList */
            eventsforPeriod.Dates = new List<EventDate>();
            /* call the method to get all the events for the supervisor */
            IEnumerable<Event> eventsForSupervisor = GetEventsForSupervisor(SupervisorID);
            /* select all the Events that are in the selected period  and sort then based on date */
            IEnumerable<Event> eventOrdering = from e in eventsForSupervisor
                                               orderby e.Date descending
                                               where e.Date >= date && e.Date <= date.AddDays(dagen)
                                               select e;
            /* new the event fill it with the elements */
            List<Event> EventsList = eventOrdering.ToList();
            /* For loop over duration for whom we are gonna get the Events */
            for(int days= 0; days < dagen; ++days)
            {
                /* new the Eventdate */
                EventDate eventDate = new EventDate();
                /* set the date of this Eventdate */
                eventDate.Datum=date.AddDays(days); 
                /* get the events that fall on this Date */
                IEnumerable<Event> eventsForDate = from e in EventsList
                                  where e.Date == date.AddDays(days)
                                  select e;
                /* to list the IEnumerable and add the Events to the eventdate */
                eventDate.Events = eventsForDate.ToList();
                /* addd this Eventdate to the list of Eventdates */
                eventsforPeriod.Dates.Add(eventDate);
            }
            return eventsforPeriod;

        }
    }
}